package com.pic.others.controller;


import com.pic.common.enums.UserType;
import com.pic.common.utils.R;
import com.pic.common.utils.UserContext;
import com.pic.others.domain.dto.user.LoginFormDTO;
import com.pic.others.domain.dto.user.LoginUserDTO;
import com.pic.others.domain.dto.user.UserDTO;
import com.pic.others.domain.dto.user.UserFormDto;
//import com.pic.others.service.ICodeService;
import com.pic.others.domain.po.User;
import com.pic.others.service.ICodeService;
import com.pic.others.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.websocket.server.PathParam;

import static com.pic.others.constants.user.UserErrorInfo.Msg.*;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
@Api(tags = "关于用户操作")
public class UserController {

    private final IUserService userService;
    private final ICodeService codeService;



    @ApiOperation("用户注册")
    @PostMapping("/register")
    public R<String> registerUser(@Valid @RequestBody UserFormDto userFormDto){
        return userService.saveUser(userFormDto);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R<LoginUserDTO>quertyUserBy(
            @Valid @RequestBody LoginFormDTO loginFormDTO){
         return userService.queryUser(loginFormDTO);
    }


    @ApiOperation("根据用户邮箱发送code")
    @GetMapping("/code/{email}")
    public R<String> sendCode(@ApiParam("用户邮箱")@PathVariable("email") String email,
                              @PathParam("number")Integer number){
        if(email==null){
            return R.error("请先填写邮箱");
        }
        return userService.sendCode(email,number);
    }


    @ApiOperation("更新用户基本信息")
    @PostMapping("/{userId}")
    public R<User> updateUser( @RequestBody UserDTO userDTO){
        log.info("{}",userDTO);
        return userService.updateUser(userDTO);
    }


    @ApiOperation("修改用户密码/重置用户密码")
    @PostMapping
    public R<String>updateUserPassword(@Valid @RequestBody UserFormDto userFormDto,
                                       @RequestParam("checkPassword") @ApiParam("确认密码") String checkPassword){
        log.info("userFormDto:{}，checkPassword:{}",userFormDto,checkPassword);
       return userService.updateUserPassword(userFormDto,checkPassword);
    }


    @ApiOperation("更新用户头像")
    @PostMapping("update/img")
    public  R<User>updateUserImg(HttpServletRequest request, MultipartFile file){
      return   userService.updateUserImg(request,file);

    }


    @ApiOperation("根据id查询用户信息")
    @GetMapping("/{userId}")
    public R<User> queryUserById(
            @ApiParam("用户id") @PathVariable("userId") long userId){
        User user =userService.getById(userId);
        if(user==null){
            return R.error(USER_ID_NOT_EXISTS);
        }
        return R.success(user);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    public R<User> getUserInfo(){
        return userService.getUserInfo();
    }












}
